package tk.uodrad.apex.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.uodrad.apex.project.pojo.dto.AssetEquipmentAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.AssetEquipmentUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.AssetEquipment;
import tk.uodrad.apex.project.pojo.vo.AssetEquipmentListVO;
import tk.uodrad.apex.project.pojo.vo.PeopleListVO;
import tk.uodrad.apex.project.service.AssetEquipmentService;
import tk.uodrad.apex.project.web.JsonResult;
import java.util.Arrays;

/**
 * 说明：
 */
@Slf4j
@RestController
@RequestMapping("/assetEquipment")
@Transactional
public class AssetEquipmentController {
    @Autowired
    private AssetEquipmentService assetEquipmentService;

    /**
     * 添加设备
     * @param assetEquipmentAddNewDTO 添加设备DTO
     * */
    @PostMapping("/add")
    public JsonResult<String> add(@RequestBody AssetEquipmentAddNewDTO assetEquipmentAddNewDTO) {
        assetEquipmentService.save(assetEquipmentAddNewDTO);
        return JsonResult.ok("添加成功");
    }
    /**
     * 根据ID查询设备
     * @param id ID
     * */
    @GetMapping("/select/{id}")
    public JsonResult<AssetEquipment> selectById(@PathVariable Long id){
        AssetEquipment data = assetEquipmentService.selectById(id);
        return JsonResult.ok(data);
    }
    /**
     * 根据ID数组删除设备
     * @param ids ID数组
     * */
    @GetMapping("/delete")
    public JsonResult<String> deleteById(Long[] ids) {
        assetEquipmentService.removeByIds(Arrays.asList(ids));
        return JsonResult.ok("删除成功！");
    }
    /**
     * 根据ID修改设备信息
     * @param assetEquipmentUpdateDTO 修改设备DTO
     * */
    @PostMapping("/update")
    public JsonResult<String> update(@RequestBody AssetEquipmentUpdateDTO assetEquipmentUpdateDTO) {
        assetEquipmentService.updateById(assetEquipmentUpdateDTO);
        return JsonResult.ok("修改成功！");
    }
    /**
     * 分页查询设备列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<AssetEquipmentListVO> AssetEquipmentListVO分页对象
     * */
    @GetMapping("/list")
    public JsonResult<IPage<AssetEquipmentListVO>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "communityCode", required = false) String communityCode) {
        IPage<AssetEquipmentListVO> list = assetEquipmentService.list(pageNum, pageSize, communityCode);
        return JsonResult.ok(list);
    }
}
